﻿using Sgr.Data;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Sgr.EntityFrameworkCore
{
    public class DefaultDbContextConfiguratorFactory : IDbContextConfiguratorFactory
    {
        private readonly IEnumerable<IDbContextConfigurator> _configurators;

        public DefaultDbContextConfiguratorFactory(IEnumerable<IDbContextConfigurator> configurators)
        {
            _configurators = configurators ?? throw new ArgumentNullException(nameof(configurators));
        }

        public IDbContextConfigurator CreateConfigurator(DatabaseType dbType)
        {
            var configurator = _configurators.FirstOrDefault(c => c.IsSupported(dbType));
            if (configurator == null)
            {
                throw new NotSupportedException($"不支持的数据库类型: {dbType}");
            }
            return configurator;
        }
    }
}